<template>
  <div class="news">
    <!-- 顶部横幅 -->
    <div class="banner">
      <h1>新着ニュース</h1>
    </div>

    <!-- 公告内容 -->
    <div class="content">
      <div class="news-list">
        <!-- In the news-item div -->
        <div
          class="news-item"
          v-for="(news, index) in paginatedNews"
          :key="index"
          @click="
            {
              this.$router.push({
                name: 'newsDetail',
                params: { id: index }, // 只传递 id 参数
                query: { newsData: JSON.stringify(news) }, // 将 news 对象作为查询参数传递
              });
            }
          "
          style="cursor: pointer"
        >
          <span class="date">{{ news.subtitle }}</span>
          <span class="notice" v-html="news.title"></span>
        </div>

        <!-- 分页 -->
        <div class="pagination">
          <button @click="prevPage" :disabled="currentPage === 1">＜</button>
          <span>{{ currentPage }}</span>
          <button @click="nextPage" :disabled="currentPage === totalPages">
            ＞
          </button>
        </div>
      </div>

      <!-- 右侧边栏 -->
      <div class="sidebar">
        <div class="archive">
          <h3>Archive</h3>
          <ul>
            <li v-for="year in archiveYears" :key="year" @click="filterByYear(year)" style="cursor: pointer;">{{ year }}</li>
          </ul>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getNewsData } from "@/api";
export default {
  name: "NewsPage",
  data() {
    return {
      currentPage: 1,
      itemsPerPage: 10,
      newsData: [{ title: "", thumbnail: "", subtitle: "", content: "" }],
      archiveYears: [],
      selectedYear: null
    };
  },
  async mounted() {
    try {
      const data = await getNewsData();
      this.newsData = data.rows;
      // 从 subtitle 中提取年份并去重
      this.archiveYears = [
        ...new Set(data.rows.map((item) => item.subtitle.split(".")[0]))
      ];
    } catch (error) {
      console.error("Error fetching news data:", error);
    }
  },
  computed: {
    totalPages() {
      return Math.ceil(this.filteredNews.length / this.itemsPerPage);
    },
    paginatedNews() {
      const start = (this.currentPage - 1) * this.itemsPerPage;
      const end = start + this.itemsPerPage;
      return this.filteredNews.slice(start, end);
    },
    filteredNews() {
      if (!this.selectedYear) {
        return this.newsData;
      }
      return this.newsData.filter((item) => item.subtitle.startsWith(this.selectedYear));
    }
  },
  methods: {
    nextPage() {
      if (this.currentPage < this.totalPages) this.currentPage++;
    },
    prevPage() {
      if (this.currentPage > 1) this.currentPage--;
    },
    filterByYear(year) {
      this.selectedYear = year;
      this.currentPage = 1;
    }
  }
};
</script>

<style scoped>
/* 页面样式 */
.news {
  background: linear-gradient(
    135deg,
    #fff 0%,
    #fff9f0 30%,
    #fff0f5 70%,
    #fff 100%
  );
  padding: 1px 0 18%;
}

/* 横幅 */
.banner {
  background: url("@/assets/1846501c44ee4d889c34bda06571b3e0.png") no-repeat
    center/cover;
  text-align: center;
  padding: 60px 0;
  color: #8d6e52;
}

.banner h1 {
  text-align: center;
  font-size: 36px;
  margin: 40px 0;
  font-family: "Hiragino Sans", "Noto Sans JP", sans-serif; /* 添加黑体字体 */
  font-weight: 400; /* 取消加粗 */
  color: #666;
}

.banner p {
  font-size: 18px;
}

/* 内容布局 */
.content {
  display: flex;
  max-width: 1200px;
  margin: 40px auto;
}

/* 公告列表 */
.news-list {
  flex: 2;
}

.news-item {
  padding: 15px;
  border-bottom: 1px solid #f08f8f;
}

.news-item .date {
  font-size: 22px;
  color: #666;
  margin-right: 10px;
  font-family: "Ming", serif;
}

.news-item .category {
  padding: 5px 10px;
  border-radius: 5px;
  color: #ca6363;
  background-color: #ffe5e5; /* 添加背景色 */
  font-size: 14px;
  margin-right: 10px;
}

.news-item .notice {
  font-size: 18px;
  color: #666; /* 灰色を追加 */
  font-family: "Ming", serif;
}

/* 分页 */
.pagination {
  text-align: center;
  margin-top: 45%;
}

.pagination button {
  padding: 5px 10px;
  margin: 0 5px;
  border: none;
  background: #d2b48c;
  color: white;
  cursor: pointer;
}

.pagination button:disabled {
  background: #ccc;
  cursor: not-allowed;
}

/* 右侧边栏 */
.sidebar {
  flex: 1;
  margin-left: 30px;
}

.archive,
.category {
  padding: 15px;
  border-radius: 10px;
  margin-bottom: 20px;
}

.archive h3,
.category h3 {
  background: #ee9089;
  color: white;
  padding: 10px;
  text-align: center;
}

.archive ul,
.category ul {
  list-style: none;
  padding: 0;
  text-align: center;
}

.archive li,
.category li {
  padding: 10px;
  border-bottom: 1px solid #e0c8a0;
}

.archive li:last-child,
.category li:last-child {
  border-bottom: none;
}
@media (max-width: 768px) {
  .news {
    padding: 1px 0 80%;
  }
}
</style>
